<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            width: 500px;
            height: 100px;
            border: 2px solid rosybrown;
        }
    </style>

</head>

<body>
    <input type="text">&nbsp;
    <button onclick="copy()">复制</button>
    <div id="box">
    </div>
    <script>
        function copy() {
            //获取输入框的值
            var inputText = document.querySelector("input").value;
            //在div下添加span
            var div = document.querySelector("#box");
            var span = document.createElement("span");
            div.appendChild(span);
            //验证span里是否有值
            if (document.querySelector("#box span").innerHTML == "") {
                //往span里加值
                var spanTextNode = document.createTextNode(inputText);
                span.appendChild(spanTextNode);
            } else {
                var flag = confirm("是否替换复制框里的内容")
                if (flag) {
                    document.querySelector("#box span").innerHTML = document.querySelector("input").value
                }

            }




        }
    </script>
</body>

</html>